package com.atguigui.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * 641.设计循环双端队列
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by WJX on 2022/8/15 9:00
 */
public class P641DesignCircularDeque {
    public static void main(String[] args) {
        MyCircularDeque solution = new P641DesignCircularDeque().new MyCircularDeque(3);
        // TO TEST
    }

    class MyCircularDeque {
        int nums;
        //之前
        List<Integer> before = null;
        //之后
        List<Integer> after = null;
        int insert;

        public MyCircularDeque(int k) {
            before = new ArrayList<>();
            after = new ArrayList<>();
            nums = k;
            insert = 0;
        }

        public boolean insertFront(int value) {
            if (insert < nums) {
                before.add(value);
                insert++;
                return true;
            }
            return false;
        }

        public boolean insertLast(int value) {
            if (insert < nums) {
                after.add(value);
                insert++;
                return true;
            }
            return false;
        }

        //删除头
        public boolean deleteFront() {
            if (insert > 0) {
                if (before.size() > 0) {
                    before.remove(before.size() - 1);
                    insert--;
                } else if (after.size() > 0) {
                    after.remove(0);
                    insert--;
                } else {
                    return false;
                }
                return true;
            }
            return false;
        }

        //删除尾
        public boolean deleteLast() {
            if (insert > 0) {
                if (after.size() > 0) {
                    after.remove(after.size() - 1);
                    insert--;
                } else if (before.size() > 0) {
                    before.remove(0);
                    insert--;
                } else {
                    return false;
                }
                return true;
            }
            return false;
        }

        //头部获得元素
        public int getFront() {
            int a = -1;
            if (insert > 0) {
                if (before.size() > 0) {
                    a = before.get(before.size() - 1);
                    return a;
                } else if (after.size() > 0) {
                    a = after.get(0);
                    return a;
                } else {
                    return a;
                }
            }
            return a;
        }

        //最后一个元素
        public int getRear() {
            int a = -1;
            if (insert > 0) {
                if (after.size() > 0) {
                    a = after.get(after.size() - 1);
                    return a;
                } else if (before.size() > 0) {
                    a = before.get(0);
                    return a;
                } else {
                    return a;
                }
            }
            return a;
        }

        public boolean isEmpty() {
            return insert == 0 ? true : false;
        }

        public boolean isFull() {
            return insert == nums ? true : false;
        }
    }

/**
 * Your MyCircularDeque object will be instantiated and called as such:
 * MyCircularDeque obj = new MyCircularDeque(k);
 * boolean param_1 = obj.insertFront(value);
 * boolean param_2 = obj.insertLast(value);
 * boolean param_3 = obj.deleteFront();
 * boolean param_4 = obj.deleteLast();
 * int param_5 = obj.getFront();
 * int param_6 = obj.getRear();
 * boolean param_7 = obj.isEmpty();
 * boolean param_8 = obj.isFull();
 */
}
